package ex1;

import ex1.sub1.CtorInit;
import ex1.sub1.MyCache;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import java.util.List;

/**
 * Hello world!
 */
public class App {
    public static void main(String[] args) {
        System.out.println("Hello World!");
//        String[] configurations = new String[]{"src/main/resources/my-spring.xml", "src/main/resources/my-spring2.xml"};

        // absolute
//        FileSystemXmlApplicationContext appCtx = new FileSystemXmlApplicationContext("file:/var/projects/ex1/src/main/resources/my-spring.xml");
        // relative
        FileSystemXmlApplicationContext appCtx = new FileSystemXmlApplicationContext("src/main/resources/my-spring.xml");
        // with list
//        FileSystemXmlApplicationContext appCtx = new FileSystemXmlApplicationContext(configurations);

        MyBean myBean = (MyBean) appCtx.getBean("yosi");

        for (String curLabel : (List<String>) myBean.getAddress().getLabels()) {
            System.out.println(curLabel);
        }

        // Spring ctx dependent design
        FishFactory ff = (FishFactory) appCtx.getBean("fishFactory");
        Fish fish1 = ff.getUnuqueFish_1();
        System.out.println(fish1.getName() + " - " + fish1.hashCode());
        Fish fish2 = ff.getUnuqueFish_1();
        System.out.println(fish2.getName() + " - " + fish2.hashCode());

        // Using lookup-method
        FishFactory_1 ff_1 = (FishFactory_1) appCtx.getBean("fishFactory_1");
        Fish fish1_1 = ff_1.getSomeFish();
        System.out.println(fish1_1.getName() + " - " + fish1_1.hashCode());
        Fish fish2_1 = ff_1.getSomeFish();
        System.out.println(fish2_1.getName() + " - " + fish2_1.hashCode());

        //Using ObjectFactory
        FishFactory_2 ff_2 = (FishFactory_2) appCtx.getBean("fishFactory_2");
        Fish fish1_2 = ff_2.getSomeFish();
        System.out.println(fish1_2.getName() + " - " + fish1_2.hashCode());
        Fish fish2_2 = ff_2.getSomeFish();
        System.out.println(fish2_2.getName() + " - " + fish2_2.hashCode());

        System.out.println("CU!");


        System.out.println("Step-2");
        FileSystemXmlApplicationContext appCtx2 = new FileSystemXmlApplicationContext("src/main/resources/my-sub-config.xml");

        MyCache myCache = (MyCache) appCtx2.getBean("my-cache");
        System.out.println(myCache.getMyCache().containsKey("one"));

        CtorInit ctorInit = (CtorInit) appCtx2.getBean("ctorInit");
        System.out.println(ctorInit.getSomeValue());

    }
}
